package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper adminMapper;

    @Override
    public DtsAdmin findByUsername(String username) {
        //根据用户名查询用户对象
        LambdaQueryWrapper<DtsAdmin> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(DtsAdmin::getUsername, username);

        DtsAdmin admin = adminMapper.selectOne(queryWrapper);

        return admin;
    }

    @Override
    public IPage<DtsAdmin> queryPage(Integer page, Integer limit, String sort, String order, String username) {
        //创建分页条件参数
        IPage<DtsAdmin> ipage = new Page (page, limit);

        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        //插件查询条件
        //如果username不为null或者  "", 则添加条件
        if(!StringUtils.isEmpty(username)) {
            qw.like("username", username);
        }

        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            } else {
                qw.orderByAsc(sort);
            }
        }


        adminMapper.selectPage(ipage , qw );

        return ipage;
    }

    @Override
    public DtsAdmin add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false); //是否被逻辑删除： false表示没有


        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(admin.getPassword());
        //要对密码进行加密
        admin.setPassword(encode);


        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        //是从前端获取的密码
        String password = admin.getPassword();
        //从数据库中获取的密码
        DtsAdmin adminDB = adminMapper.selectById(admin.getId());
        String passwordDb = adminDB.getPassword();
        //如果从前端获取密码与数据库中的密文相同， 说明修改时前端的密码没改
        //如果从前端获取密码与数据库中的密文不相同， 说明修改时密码修改了
        if(!password.equalsIgnoreCase(passwordDb)){
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encode = passwordEncoder.encode(admin.getPassword());
            admin.setPassword(encode);
        }

        admin.setUpdateTime(new Date());

        adminMapper.updateById(admin);

        return admin;
    }
}
